package ru.home.documentmanager.dao.impl;

import org.hibernate.Hibernate;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ru.home.documentmanager.dao.UserDAO;
import ru.home.documentmanager.model.User;

@Repository("userDAO")
public class UserDAOImpl extends AbstractDAO<User> implements UserDAO {

    private static final String USERNAME = "username";

    public UserDAOImpl() {
        super(User.class);
    }

    @Override
    public User findByUsername(String username) {
        return (User) getSession().createCriteria(User.class)
                .add(Restrictions.eq(USERNAME, username)).uniqueResult();
    }

    @Override
    public void initFiles(User user) {
        Hibernate.initialize(user.getFiles());
    }

}
